package com.yh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.bean.BBS;
import com.yh.bean.Plan;
import com.yh.service.PlanService;

@Controller
public class PlanController {
	
	@Autowired
	@Qualifier("plan")
	private PlanService service;
	
	//首页面查询
	@RequestMapping("queryPlan")
	@ResponseBody
	public Map queryplan(int curr,HttpSession session){
		//前台获取	id当做参数传入后台运行
		int pl_userid=(int) session.getAttribute("userid");
		Map map=new HashMap();
		Page page=PageHelper.startPage(curr,4,"pl_id desc");	
		List<Plan> plist=service.queryplan(pl_userid);
		//total和mlist存贮到map集合中 发送到前台页面
		map.put("total", page.getTotal());	//获取总条数
		map.put("plist", plist);
		return map;
	}
	
	//详情显示
	@RequestMapping("/showPlan")
	@ResponseBody
	public Plan showplan(int pl_id,HttpSession session){
		Plan plan=service.showplan(pl_id);
		String pl_username=(String) session.getAttribute("username");
		plan.setPl_username(pl_username);
		return plan;
	}
	
	//添加计划
	@RequestMapping("/insertPlan")
	@ResponseBody
	public int insertplan(Plan plan,HttpSession session){
		//获取session中用户id
		int pl_userid=(int) session.getAttribute("userid");
		plan.setPl_state("未完成");
		plan.setPl_userid(pl_userid);
		System.out.println(plan);
		int a=service.insertplan(plan);
		return a;
	}
	
	/*
	 * 修改计划状态
	 */
	@RequestMapping("/updateState")
	@ResponseBody
	public int updatestate(int pl_id){
		int a=service.updatestate(pl_id);
		return a;
	}
	
	/*
	 * 修改计划信息
	 */
	@RequestMapping("/updatePlan")
	@ResponseBody
	public int updateplan(Plan plan){
		int a=service.updateplan(plan);
		return a; 
	}
	
	/*
	 * 批量删除
	 */
	@RequestMapping("/deletePlan")
	@ResponseBody
	public int deleteplan(@RequestParam("arr[]")String[] arr){
		for(String a:arr){
			System.out.println(a);
		}
		int aa=service.deleteplan(arr);
		return aa;
	}
	
}
